package com.ncmem.up7.sql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class SqlWhereMerge {
    Map<String, String> m_cds;

    public SqlWhereMerge()
    {
        this.m_cds = new HashMap<String, String>();
    }

    public void equal(String n,String v)
    {
        if(StringUtils.isEmpty(v.trim())) return;
        this.m_cds.put(n, String.format("%s='%s'", n,v));
    }

    /**
     *
     * @param n
     * @param v
     * @param ignore 是否忽略空值
     */
    public void equal(String n,String v,boolean ignore)
    {
        if(StringUtils.isEmpty(v.trim()))
        {
            if(ignore) return;
        }
        this.m_cds.put(n, String.format("%s='%s'", n,v));
    }

    public void equal(String n,int v)
    {
        this.m_cds.put(n, String.format("%s=%d", n,v));
    }
    public void del(String n)
    {
        this.m_cds.remove(n);
    }

    public void charindex(String n,String column)
    {
        String sql = String.format("charindex('%s',%s)=1",n,column);
        this.m_cds.put("", sql);
    }

    public void instr(String n,String column)
    {
        String sql = String.format("instr(%s,'%s')=1",column,n);
        this.m_cds.put("", sql);
    }

    public void like(String n,String v)
    {
        if(StringUtils.isEmpty(v.trim())) return;
        this.m_cds.put(n, String.format("%s like '%%s%'", n,v));
    }

    public void clear() {this.m_cds.clear();}

    public String to_sql()
    {
        ArrayList<String> cs = new ArrayList<String>();
        for(Object obj : this.m_cds.keySet())
        {
            Object value = this.m_cds.get(obj );
            cs.add(value.toString());
        }

        String[] s = cs.toArray(new String[cs.size()]);

        return StringUtils.join(s, " and ");
    }
}
